---@class BuffManager
---@field  getBuffsByModel fun(fighter,modelId,getPermanent,source):FighterBuff[]
---@field getBuffByModel fun():FighterBuff
BuffManager = ClientFight.CreateClass("BuffManager")
local this = BuffManager
---获取buff配置 ,
---     * 这里需要一个 source 的原因是 , 奇异碎片的buff 是有动态参数的
---     * 所以在这里就需要把动态参数给替换掉 2021-05-13
---@param source Fighter
function this.getBuffBean(id, source)
    local t_buffBean = source.buffDynamic[id]
    if t_buffBean ~= nil then
        return t_buffBean
    end
    local bean = ClientFight.DataManager.t_buffBeanMap[id]
    if bean == nil then
        logError("T_buffBean template not found, id: " .. id)
        return nil
    end
    if not bean.dynamicParamBuff then
        return bean
    end
    bean = bean:cloneInstance()
    local b = bean:dynamicParamParse(source)
    if not b then
        logError("T_buffBean dynamicParamParse error, id: " .. id)
        return nil
    end
    --成功了, 缓存这个buff, 没必要每次使用都去动态解析
    source.buffDynamic[bean.f_BuffId] = bean
    return bean
end
---添加buff
---@param fight Fight
---@param skill FightUsingSkill
---@param source Fighter
---@param target Fighter
function this.addBuff(fight, skill, source, target, permanent, buffModelId, buffOverlay, ...)
    local resultMap = {}
    local addParameters = { ... }
    --2021-05-17,召唤物,如果有这个状态,强行无视任何buff
    if target.summons then
        if source ~= target then
            if FighterStateEnum.is(target, FighterStateEnum.ATTRACT_NORMAL_BULLET) then
                SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. buffModelId .. "特殊状态吸引普攻子弹的召唤物,无视任何buff ");
                resultMap[1] = IBuffEffect.FAIL
                return resultMap
            end
        end
    end
    local buffBean = this.getBuffBean(buffModelId, source)
    if buffBean == nil then
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. buffModelId .. "找不到 buff ID ");
        resultMap[1] = IBuffEffect.FAIL
        return resultMap
    end
    if buffBean.f_BuffProbability > 0 then
        local probability = buffBean.f_BuffProbability
        local random = fight:random(Global.TEN_THOUSANDTH)
        if (random >= probability) then
            SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. buffModelId ..
                    "因为概率添加失败 配置概率:" .. probability .. ",随机概率:" .. random
            );
            resultMap[1] = IBuffEffect.FAIL
            return resultMap
        end
    end
    ---@type Fighter
    local adder = nil
    if buffBean.f_Target == BuffTargetTypeEnum.ONESELF then
        adder = source
    else
        adder = target
    end

    if adder == nil then
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]发生错误, " .. buffModelId .. "添加的时候目标不存在");
        resultMap[1] = IBuffEffect.FAIL
        return resultMap
    end

    if adder:isDie() then
        local logAdder = adder
        SkillRoleMgr.savePathFile(
                "[" .. fight.frame .. "][BUFF]" .. buffModelId .. " 添加的时候目标已经死亡:" .. logAdder.fighterId);
        resultMap[1] = IBuffEffect.FAIL
        return resultMap
    end
    -- FIXME: 2020/11/24 wangjunhui 这边互斥有问题 例如已经有一个霸体buff  如果不想让其他buff加上来需要在其他buff的互斥tag上加上霸体标记  这里需不需要改？
    if buffBean.f_Exclusion ~= nil then
        for k, tag in pairs(buffBean.f_Exclusion) do
            if (adder.totalSign.signs[tag]) then
                local finalAdder = adder
                SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. finalAdder.fighterId .. "添加" .. buffModelId .. "的时候因为状态" .. tag .. "互斥而添加失败");
                resultMap[1] = IBuffEffect.FAIL
                this.onExclusion(buffBean.f_Exclusion, adder, source, fight)
                return resultMap
            end
        end
    end
    --按类型获取Buff
    ---@type FighterBuff
    local buff = FighterBuff.New()
    buff.id = fight.buffIds
    fight.buffIds = fight.buffIds + 1
    --buff模型id
    buff.modelId = buffBean.f_BuffId
    --开始帧数
    buff.startFrame = fight.frame
    --上一次作用帧数
    buff.actionFrame = fight.frame
    --持续时间
    buff.continuedTime = buffBean.f_BuffRound
    --设置层数
    buff.overlay = buffOverlay
    --设置buff配置
    buff.buffBean = buffBean
    --所属技能
    buff.skill = skill
    --来源
    buff.source = source
    --设置目标
    buff.target = adder
    --设置是否永久
    buff.permanent = permanent
    --设置添加参数
    buff.addParameters = addParameters
    ---@type FightResult
    local fightResult = FightResult.New()
    EventManager.notifyEventListener(fight, FightEventEnum.BEFORE_ADD_BUFF, adder.fighterId, buff, fightResult)
    if fightResult:getState() == DamageResultTypeEnum.IMMUNITY then
        --被免疫掉了本次buff
        resultMap[1] = IBuffEffect.FAIL
        return resultMap
    end
    if not this.canAddBuff(buff) then
        resultMap[1] = IBuffEffect.FAIL
        return resultMap
    end

    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. "准备增加 buff:" .. buffModelId .. " , BUFFID:" .. buff.id ..
            "scoreFightId:" .. source.fighterId .. ",targetFightId:" .. target.fighterId
    );

    if buffBean.f_Replace == BuffReplaceTypeEnum.COEXIST then
        local result = BuffEffectManager.addBuffEffect(fight, buff)
        if bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0 then
            local buffs = this.getBuffsByModel(adder, buffModelId, true)
            if table.getn(buffs) >= buffBean.f_MaxReplaceLyer then
                --删掉最早的一个
                table.sort(buffs, function(a, b)
                    return a.startFrame < b.startFrame
                end)
                this.removeFighterBuff(fight, adder, buffs[1], true)
            end
            --永久还是临时
            this.addFighterBuff(fight, adder, buff, false)
        end
        resultMap[1] = result
        resultMap[2] = buff
        return resultMap
    end
    local operate = BuffReplaceTypeEnum.DROP
    if buffBean.f_Replace == BuffReplaceTypeEnum.OVERLAY then
        operate = BuffReplaceTypeEnum.OVERLAY
    elseif buffBean.f_Replace == BuffReplaceTypeEnum.REPLACE then
        operate = BuffReplaceTypeEnum.REPLACE
    end
    --效果叠加
    if operate == BuffReplaceTypeEnum.OVERLAY then
        ---@type FighterBuff
        local oldBuff = this.getBuffByModel(adder, buffBean, source, permanent)
        if oldBuff == nil then
            oldBuff = this.getBuffByModel(adder, buffBean, source, not permanent)
        end
        if oldBuff == nil then
            local result = BuffEffectManager.addBuffEffect(fight, buff)
            if bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0 then
                --永久还是临时
                this.addFighterBuff(fight, adder, buff, false)
            end
            resultMap[1] = resultMap
            resultMap[2] = buff
            return resultMap
        end
        --层数相加
        oldBuff.overlay = oldBuff.overlay + buff.overlay
        --最大层数
        if oldBuff.overlay > buffBean.f_MaxReplaceLyer then
            oldBuff.overlay = buffBean.f_MaxReplaceLyer
        end
        --重置时间
        oldBuff.startFrame = fight.frame
        oldBuff.actionFrame = fight.frame
        oldBuff.overlaypParameters = addParameters
        --重置效果
        local result = BuffEffectManager.addBuffEffect(fight, oldBuff)
        local logBuff = oldBuff
        SkillRoleMgr.savePathFile(
                "[" .. fight.frame .. "][BUFF]层数" .. buff.overlay .. "叠加到对象"
                        .. logBuff.target.fighterId .. "身上的BUFF:" .. logBuff.modelId .. ",层数叠加结果："
                        .. result .. ",叠加后:" .. logBuff.overlay);
        if bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0 then
            --永久还是临时
            this.addFighterBuff(fight, oldBuff.target, oldBuff, true);
        end
        resultMap[1] = result
        resultMap[2] = oldBuff
        return resultMap
    elseif operate == BuffReplaceTypeEnum.REPLACE then
        --效果替换
        ---@type FighterBuff
        local oldBuff = this.getBuffByLayer(adder, buffBean, source, permanent)
        if oldBuff == nil then
            local result = BuffEffectManager.addBuffEffect(fight, buff)
            if bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0 then
                --永久还是临时
                this.addFighterBuff(fight, adder, buff, false)
            end
            resultMap[1] = result
            resultMap[2] = buff
            return resultMap
        end
        --替换优先级小的无法替换大的
        if buff.buffBean.f_RepalcePriority < oldBuff.buffBean.f_RepalcePriority then
            resultMap[1] = IBuffEffect.FAIL
            return resultMap
        end
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. target.fighterId .. "移除原有buff:"
                .. oldBuff.modelId);
        --移除原有buff
        this.removeFighterBuff(fight, adder, oldBuff, true, oldBuff.addParameters)
        --添加BUFF效果
        local result = BuffEffectManager.addBuffEffect(fight, buff)
        if bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0 then
            --永久还是临时
            this.addFighterBuff(fight, adder, buff, false)
        end
        resultMap[1] = result
        resultMap[2] = buff
        return resultMap
    elseif operate == BuffReplaceTypeEnum.DROP then
        --丢弃
        ---@type FighterBuff
        local oldBuff = this.getBuffByModel(adder, buffBean, source, permanent)
        if oldBuff == nil then
            local result = BuffEffectManager.addBuffEffect(fight, buff);
            if (bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0) then
                --永久还是临时
                this.addFighterBuff(fight, adder, buff, false);
            end
            resultMap[1] = result
            resultMap[2] = buff
            return resultMap;
        end
    end
    resultMap[1] = IBuffEffect.FAIL
    return resultMap
end
---@param buff FighterBuff
function this.canAddBuff(buff)
    local effect = BuffEffectManager.getBuffEffect(buff.buffBean.f_BuffEffect)
    if effect then
        local force = effect:canAddBuffForce(buff)
        if force then
            return true
        end
    elseif buff.buffBean.f_BuffEffect > 0 then
        logError("找不到BuffEffect by Id:" .. buff.buffBean.f_BuffEffect)
    end
    return BuffConditionType:action(buff, buff.buffBean.f_BuffAddCondition, buff.addParameters);
end
---被互斥标记给抵挡了
function this.onExclusion(f_exclusion, adder, source, fight)
    if this.onExclusionByImmune(f_exclusion, adder, source, fight) then
        return
    end
end
---是否被霸体互斥了
---@param f_exclusion number[]
---@param adder Fighter
---@param source Fighter
---@param fight Fight
function this.onExclusionByImmune(f_exclusion, adder, source, fight)
    local immuneSelf = adder.totalSign.signs[FighterSignEnum.IMMUNE] or false
    if not immuneSelf then
        return false
    end
    local immune = table.contains(f_exclusion, FighterSignEnum.IMMUNE)
    if not immune then
        return immune
    end
    local frameInfoCommand = ReportManager.additionalStateToJsonString(fight, adder, FighterAdditionalState.IMMUNE)
    table.insert(fight.actions, frameInfoCommand)
    FightManager.playActions(frameInfoCommand)
    return immune
end
---@param fighter Fighter
---@return FighterBuff[]
function this.getBuffsByEffect(fighter, buffEffect)
    local list = {}
    for k, buff in pairs(fighter.buffs) do
        if buff.buffBean.f_BuffEffect == buffEffect then
            table.insert(list, buff)
        end
    end
    return list
end
---@param fighter Fighter
---@return FighterBuff[]
function this.getBuffsByHelpful(fighter, helpful, getPermanent)
    local list = {}
    for k, buff in pairs(fighter.buffs) do
        if buff.buffBean.f_BuffType == helpful then
            table.insert(list, buff)
        end
    end
    if getPermanent then
        for k, buff in pairs(fighter.permanentBuffs) do
            if buff.buffBean.f_BuffType == helpful then
                table.insert(list, buff)
            end
        end
    end
    return list
end
---@param fighter Fighter
---@return FighterBuff[]
function this.getBuffsByControl(fighter, getPermanent)
    local list = {}
    for k, buff in pairs(fighter.buffs) do
        if table.contains(buff.buffBean.f_Control, 1) then
            table.insert(list, buff)
        end
    end
    if getPermanent then
        for k, buff in pairs(fighter.permanentBuffs) do
            if table.contains(buff.buffBean.f_Control, 1) then
                table.insert(list, buff)
            end
        end
    end
    return list
end
---@param fighter Fighter
---@return FighterBuff[]
function this.getBuffsByHelpfulOrControl(fighter, helpful, getPermanent)
    if helpful == 1 or helpful == 2 then
        return this.getBuffsByHelpful(fighter, helpful, getPermanent)
    elseif helpful == 3 then
        return this.getBuffsByControl(fighter, getPermanent)
    end
end
---获取buff FIXME: 2020/11/10 wangjunhui buff可以并列存在  建议使用getBuffsByModel()
---@private
---@param fighter Fighter
---@return FighterBuff
function this.getBuffByModel1(fighter, modelId)
    local buffs = fighter.buffs
    for k, buff in pairs(buffs) do
        if buff.modelId == modelId then
            return buff
        end
    end
    buffs = fighter.permanentBuffs
    for k, buff in pairs(buffs) do
        if (buff.modelId == modelId) then
            return buff
        end
    end
    return nil
end
---@private
---@param fighter Fighter
---@param buffBean T_buffBean
---@param source Fighter
function this.getBuffByModel2(fighter, buffBean, source, permanent)
    local buffs = fighter.buffs
    if permanent then
        buffs = fighter.permanentBuffs
    end
    for k, buff in pairs(buffs) do
        local continue = false
        if buff.modelId ~= buffBean.f_BuffId then
            continue = true
        end
        if buffBean.f_BuffAll == BuffReplaceStyleEnum.PERSON and source ~= nil then
            if buff.source.fighterId ~= source.fighterId then
                continue = true
            end
        end
        if not continue then
            return buff
        end
    end
    return nil
end
addOverload(this, "getBuffByModel", this.getBuffByModel1, "table", "number")
addOverload(this, "getBuffByModel", this.getBuffByModel2, "table", "table", "table", "boolean")

---@private
---@param fighter Fighter
---@return FighterBuff[]
function this.getBuffsByModel1(fighter, modelId, getPermanent)
    local buffs = fighter.buffs
    local list = {}
    for k, buff in pairs(buffs) do
        if buff.modelId == modelId then
            table.insert(list, buff)
        end
    end
    if getPermanent then
        buffs = fighter.permanentBuffs
        for k, buff in pairs(buffs) do
            if (buff.modelId == modelId) then
                table.insert(list, buff)
            end
        end
    end
    return list
end
---@private
---@param fighter Fighter
---@param source Fighter
---@return FighterBuff[]
function this.getBuffsByModel2(fighter, modelId, getPermanent, source)
    local buffs = fighter.buffs
    local list = {}
    for k, buff in pairs(buffs) do
        local continue = false
        if buff.modelId ~= modelId then
            continue = true
        end
        if buff.buffBean.f_BuffAll == BuffReplaceStyleEnum.PERSON and source ~= nil then
            if buff.source.fighterId ~= source.fighterId then
                continue = true
            end
        end
        if not continue then
            table.insert(list, buff)
        end
    end
    if getPermanent then
        buffs = fighter.permanentBuffs
        for k, buff in pairs(buffs) do
            local continue = false
            if buff.modelId ~= modelId then
                continue = true
            end
            if buff.buffBean.f_BuffAll == BuffReplaceStyleEnum.PERSON and source ~= nil then
                if buff.source.fighterId ~= source.fighterId then
                    continue = true
                end
            end
            if not continue then
                table.insert(list, buff)
            end
        end
    end
    return list
end
addOverload(this, "getBuffsByModel", this.getBuffsByModel1, "table", "number", "boolean")
addOverload(this, "getBuffsByModel", this.getBuffsByModel2, "table", "number", "boolean", "table")
---获取buff
---@param fighter Fighter
---@param buffBean T_buffBean
---@param source Fighter
function this.getBuffByLayer(fighter, buffBean, source, permanent)
    local buffs = fighter.buffs
    if (permanent) then
        buffs = fighter.permanentBuffs
    end
    for k, buff in pairs(buffs) do
        local continue = false
        if buffBean.f_RepalceLyer == 0 and buff.modelId ~= buffBean.f_BuffId then
            continue = true
        end
        if buffBean.f_RepalceLyer ~= 0 and buff.buffBean.f_RepalceLyer ~= buffBean.f_RepalceLyer then
            continue = true
        end
        if buffBean.f_BuffAll == BuffReplaceStyleEnum.PERSON and source then
            if buff.source.fighterId ~= source.fighterId then
                continue = true
            end
        end
        if not continue then
            return buff
        end
    end
    return nil
end
---增加buff
---@param fight Fight
---@param fighter Fighter
---@param buff FighterBuff
function this.addFighterBuff(fight, fighter, buff, isOverlay)
    local result = false
    local buffBean = buff.buffBean
    ---@type table<number,FighterBuff>
    local buffs
    if buff.permanent then
        buffs = fighter.permanentBuffs
    else
        buffs = fighter.buffs
    end
    if not buffs[buff.id] then
        buffs[buff.id] = buff
        result = true;
    end
    local frameInfoCommand = {};
    if isOverlay then
        frameInfoCommand = ReportManager.buffToJsonString(fight, buff, 2);
    else
        frameInfoCommand = ReportManager.buffToJsonString(fight, buff, 0);
    end
    table.insert(fight.actions, frameInfoCommand)
    FightManager.playActions(frameInfoCommand)
    SkillRoleMgr.savePathFile(string.format("[%s][BUFF]%s添加%s结果%s", fight.frame, fighter.fighterId, buff.modelId, tostring(result)));
    if result then
        if buff.buffBean.f_BuffActionTrigger > 0 then
            local listener = this._innerActionListener.New(fighter, fight, buff)
            EventManager.addEventListener(fight, buff.buffBean.f_BuffActionTrigger, fighter.fighterId, listener)
        end
        if buff.buffBean.f_BuffRemoveTrigger > 0 then
            local removeTrigger = true;
            if (buff.buffBean.f_BuffEffect == 67) then
                if (buff.target ~= buff.source) then
                    removeTrigger = false;
                end
            end
            if (removeTrigger) then
                local listener = this._innerRemoveListener.New(fighter, fight, buff)
                EventManager.addEventListener(fight, buff.buffBean.f_BuffRemoveTrigger, fighter.fighterId, listener)
            end
        end

    end
    --计算属性变化
    if not table.getn(buff.attributes.attributes) or buffBean.f_BuffSpecialEffect ~= nil or buffBean.f_Tag ~= nil then
        FighterAttributeManager.countFighterAttribute(fight, fighter, CalculatorTypeEnum.BUFF)
    end
    local target = buff.target
    --        //TODO: 2020/11/24 wangjunhui 以下逻辑需要改一下  按照新加入的buff互斥标记移除原有的buff
    --        // 移除此人身上所有标记互斥BUFF
    ---@type FighterBuff[]
    local buffsOfTarget = {}
    table.addAllToArr(buffsOfTarget, target.buffs)
    if #buffsOfTarget > 0 then
        for k, _buff in pairs(buffsOfTarget) do
            if this.inArray(_buff.buffBean.f_Exclusion, buff.buffBean.f_Tag) then
                SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. target.fighterId .. "因为标记互斥被移除buff"
                        .. _buff.modelId);
                this.removeFighterBuff(fight, target, _buff, true, _buff.addParameters);
            end
        end
    end
    --触发添加BUFF事件
    EventManager.notifyEventListener(fight, FightEventEnum.ADD_BUFF, fighter.fighterId, buff, result)
    local effect = BuffEffectManager.getBuffEffect(buff.buffBean.f_BuffEffect)
    effect:afterAdd(fight, buff)--这一行一定要在最后
    return result
end

function this.inArray(array, values)
    if array == nil or values == nil then
        return false
    end
    for k, v in pairs(array) do
        for k2, v2 in pairs(values) do
            if v == v2 then
                return true
            end
        end
    end
    return false
end
---@param fight Fight
---@param fighter Fighter
function this.removeFightBuff(fight, fighter, buffId, force, ...)
    local buff = this.getBuffByModel(fighter, buffId)
    if buff ~= nil then
        this.removeFighterBuff(fight, fighter, buff, force, ...);
    end
end
---移除buff force 强制移除 不去判断移除条件
---@param fight Fight
---@param fighter Fighter
---@param buff FighterBuff
function this.removeFighterBuff(fight, fighter, buff, force, ...)
    --移除buff效果
    if buff.removeEffect then
        return
    end
    local buffBean = buff.buffBean
    local attrChange = false
    if not table.getn(buff.attributes.attributes) or buffBean.f_BuffSpecialEffect ~= nil or buffBean.f_Tag ~= nil then
        attrChange = true
    end
    local result = BuffEffectManager.removeBuffEffect(fight, buff, force, ...)
    if not force and (bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) == 0) then
        return
    end
    local remove = nil
    if buff.permanent then
        remove = fighter.permanentBuffs[buff.id]
        fighter.permanentBuffs[buff.id] = nil
    else
        remove = fighter.buffs[buff.id]
        fighter.buffs[buff.id] = nil
    end
    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][BUFF]" .. fighter.fighterId .. "移除" .. buff.modelId
            .. "成功" .. tostring(logRemove ~= nil));
    if buff.buffBean.f_BuffRemoveTrigger > 0 then
        EventManager.removeEventListener(fight, buff.buffBean.f_BuffRemoveTrigger, fighter.fighterId,
                buff.id .. "_REMOVE");
    end
    if buff.pool.type == BuffPoolEnum.DEFEAT then
        fighter.defeatPool = fighter.defeatPool - buff.pool.current
    end
    --计算属性变化
    if attrChange then
        FighterAttributeManager.countFighterAttribute(fight, fighter, CalculatorTypeEnum.BUFF)
    end
    --触发移除BUFF事件
    EventManager.notifyEventListener(fight, FightEventEnum.REMOVE_BUFF, fighter.fighterId, buff)
    if buff.buffBean.f_BuffActionTrigger > 0 then
        EventManager.removeEventListener(fight, buff.buffBean.f_BuffActionTrigger, fighter.fighterId, buff.id .. "_ACTION")
    end
    local frameInfoCommand = ReportManager.buffToJsonString(fight, buff, 1);
    table.insert(fight.actions, frameInfoCommand)
    FightManager.playActions(frameInfoCommand)
    local effect = BuffEffectManager.getBuffEffect(buff.buffBean.f_BuffEffect);
    effect:afterRemove(fight, buff);--这一行一定要在最后
    local source = buff.source
    if source == buff.target then
        return
    end
    --我移除的时候,通知一下给我加这个buff的施法者, 我移除了
    EventManager.notifyEventListener(fight, FightEventEnum.REMOVE_BUFF_TARGET, source.fighterId, buff)
end

---内部类
this._innerActionListener = ClientFight.CreateClass("_innerActionListener", nil, ClientFight.IFightEventListener)
---@class BuffManager._innerActionListener:IFightEventListener
local _innerLis = this._innerActionListener
function _innerLis:ctor(fighter, fight, buff)
    ---@type Fighter
    self.fighter = fighter
    ---@type Fight
    self.fight = fight
    ---@type FighterBuff
    self.buff = buff
end
function _innerLis:notify(eventId, ...)
    local f_BuffType = self.buff.f_BuffType
    if (f_BuffType == BuffTypeEnum.BENEFICIAL) then
        if FighterStateEnum.is(self.fighter, FighterStateEnum.IMMUNITY_BENEFICIAL_BUFF) then
            return
        end
    elseif f_BuffType == BuffTypeEnum.NEGATIVE then
        if FighterStateEnum.is(self.fighter, FighterStateEnum.IMMUNITY_DEBUFF) then
            return
        end
    end
    --未到作用时间
    if self.buff.buffBean.f_BuffTime > 0 then
        --间隔作用buff，第一次添加的是否不作用
        if self.buff.actionFrame == self.fight.frame then
            return
        end
        local frame = self.buff.buffBean.f_BuffTime / Global.FIGHT_FRAME
        if (frame > 0 and (self.fight.frame - self.buff.actionFrame) % frame ~= 0) then
            return
        end
    end
    local result = BuffEffectManager.actionBuffEffect(self.fight, self.buff, ...)
    if bit.band(result, IBuffEffect.REMOVE) > 0 then
        BuffManager.removeFighterBuff(self.fight, self.fighter, self.buff, false, ...)
    elseif bit.band(result, IBuffEffect.REMOVE_FORCE) > 0 then
        BuffManager.removeFighterBuff(self.fight, self.fighter, self.buff, true, ...)
    elseif bit.band(result, IBuffEffect.SUCCESS_ATTRI_HAS_CHANGE) > 0 then
        -- 计算属性变化
        FighterAttributeManager.countFighterAttribute(self.fight, self.fighter, CalculatorTypeEnum.BUFF);
    end
end
function _innerLis:getId()
    return self.buff.id .. "_ACTION";
end
function _innerLis:priority()
    return self.buff.buffBean.f_Priority * 100000 + self.buff.id * 10 + 1
end
---内部类
this._innerRemoveListener = ClientFight.CreateClass("_innerRemoveListener", nil, ClientFight.IFightEventListener)
---@class BuffManager._innerRemoveListener:IFightEventListener
local _innerLis1 = this._innerRemoveListener
function _innerLis1:ctor(fighter, fight, buff)
    ---@type Fighter
    self.fighter = fighter
    ---@type Fight
    self.fight = fight
    ---@type FighterBuff
    self.buff = buff
end
function _innerLis1:notify(eventId, ...)
    --未到移除时间
    if self.buff.continuedTime > 0 then
        local frame = self.buff.continuedTime / Global.FIGHT_FRAME
        if frame > 0 and self.fight.frame < self.buff.startFrame + frame then
            return ;
        end
    end
    -- 移除原有buff
    SkillRoleMgr.savePathFile("[" .. self.fight.frame .. "][BUFF]" .. self.fighter.fighterId ..
            "触发事件移除buff:" .. self.buff.modelId .. "  移除时机:" .. self.buff.buffBean.f_BuffRemoveTrigger);
    BuffManager.removeFighterBuff(self.fight, self.fighter, self.buff, false, ...);
end
function _innerLis1:getId()
    return self.buff.id .. "_REMOVE";
end
function _innerLis1:priority()
    return self.buff.buffBean.f_Priority * 100000 + self.buff.id * 10
end

